package com.ayuer.chapter07;

import java.util.*;

public class Leetcode819 {
    public static void main(String[] args) {
        String paragraph = "Bob hit a ball, the hit BALL flew far after it was hit.";
        String[] banned = {"hit"};
        mostCommonWord(paragraph, banned);
    }

    public static String mostCommonWord(String paragraph, String[] banned) {

        HashMap<String, Integer> map = new HashMap<>();
        Set<String> bannedSet = Set.of(banned);
        paragraph = paragraph.toLowerCase();
        String[] words = paragraph.split("[^a-zA-Z]+");
        for (String word : words) {
            if (bannedSet.contains(word)) {
                continue;
            }
            if (!map.containsKey(word)) {
                map.put(word, 1);
            } else {
                map.put(word, map.get(word) + 1);
            }
        }
        Optional<Map.Entry<String, Integer>> optional = map.entrySet().stream().max(Map.Entry.comparingByValue());
//        map.forEach((k, v) -> {
//            if (v > max) {
//                max = v;
//                maxStr = k;
//            }
//        });
        return optional.map(Map.Entry::getKey).orElse(null);
    }

}
